import os
import sys
import pymysql
import pickle
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, pyqtSignal, QRegExp
from PyQt5.QtGui import QFont, QIcon, QRegExpValidator, QPalette
from login.register_page import RegisterPage
from admin.admin_control import AdminControl
from user.user_control import UserControl

class LoginPage(QWidget):
    def __init__(self):
        super(LoginPage, self).__init__()
        self.setWindowTitle('智能考勤系统')
        self.setWindowIcon(QIcon("./gui_image/gui_Icon.png"))
        self.setFixedSize(400, 200)
        self.init_ui()
        self.user_control = UserControl()

    def init_ui(self):
        # UI初始化代码（保持原代码不变）
        self.label_0 = QLabel('用户登录页面')
        self.label_0.setAlignment(Qt.AlignCenter)
        self.label_0.setFont(QFont('宋体', 12, QFont.Bold))
        
        self.id_label = QLabel('用户账号')
        self.password_label = QLabel('密码')
        self.id_line = QLineEdit()
        self.password_line = QLineEdit()
        
        self.line_init()
        
        self.login_button = QPushButton('登录')
        self.register_button = QPushButton('注册')
        self.exit_button = QPushButton('退出')
        
        self.remember_id = QCheckBox('记住用户账号')
        self.remember_password = QCheckBox('记住密码')
        self.auto_login = QCheckBox('自动登录')
        
        self.checkbox_init()
        self.pushbutton_init()
        self.layout_init()
        self.login_init()

    # 其他方法保持原样...
    def line_init(self):
        self.id_line.setPlaceholderText('在此输入用户账号')
        self.password_line.setPlaceholderText('在此输入密码')
        self.password_line.setEchoMode(QLineEdit.PasswordEchoOnEdit)
        self.id_line.textChanged.connect(self.check_input)
        self.password_line.textChanged.connect(self.check_input)

    def pushbutton_init(self):
        self.login_button.setEnabled(False)
        self.login_button.clicked.connect(self.do_login)
        self.register_button.clicked.connect(self.do_register)
        self.exit_button.clicked.connect(self.close)

    def checkbox_init(self):
        self.remember_id.stateChanged.connect(self.remember_id_func)
        self.remember_password.stateChanged.connect(self.remember_password_func)
        self.auto_login.stateChanged.connect(self.auto_login_func)

    def layout_init(self):
        # 布局代码保持原样...
        pass

    def check_input(self):
        if self.id_line.text() and self.password_line.text():
            self.login_button.setEnabled(True)
        else:
            self.login_button.setEnabled(False)

    def do_login(self):
        # 登录逻辑保持原样...
        pass

    def do_register(self):
        register_page = RegisterPage()
        register_page.exec()

    def remember_id_func(self):
        if self.remember_id.isChecked():
            name = self.id_line.text()
            with open('./autoLoginFile/login.pkl','wb') as f:
                pickle.dump(name,f)
        else:
            with open('./autoLoginFile/login.pkl','wb') as f:
                pickle.dump(0,f)

    def remember_password_func(self):
        if self.remember_password.isChecked():
            self.remember_id.setChecked(True)
            data = [self.id_line.text(), self.password_line.text()]
            with open('./autoLoginFile/login.pkl','wb') as f:
                pickle.dump(data,f)

    def auto_login_func(self):
        if self.auto_login.isChecked():
            data = [self.id_line.text(), self.password_line.text()]
            os.remove('./autoLoginFile/auto.pkl')
            with open('./autoLoginFile/auto.pkl','wb') as f:
                pickle.dump(data,f)
        else:
            with open('./autoLoginFile/auto.pkl','wb') as f:
                pickle.dump(0,f)

    def login_init(self):
        try:
            with open('./autoLoginFile/login.pkl','rb') as f:
                data = pickle.load(f)
            if isinstance(data,str):
                self.id_line.setText(data)
                self.remember_id.setChecked(True)
            else:
                self.id_line.setText(data[0])
                self.password_line.setText(data[1])
                self.remember_id.setChecked(True)
                self.remember_password.setChecked(True)
        except:
            pass
        
        try:
            with open('./autoLoginFile/auto.pkl','rb') as f:
                data2 = pickle.load(f)
            self.id_line.setText(data2[0])
            self.password_line.setText(data2[1])
            self.do_login()
        except:
            pass

    def check_login_state(self):
        self.remember_id_func()
        self.remember_password_func()
        self.auto_login_func()

    def jump2admin(self):
        admin_dialog = QDialog()
        admin_dialog.setWindowTitle('管理员页面')
        admin_dialog.setWindowIcon(QIcon("./gui_image/gui_Icon.png"))
        v_layout = QVBoxLayout(admin_dialog)
        admin_control = AdminControl()
        v_layout.addWidget(admin_control)
        admin_dialog.exec()
        self.show()

    def jump2user(self):
        ID = self.id_line.text()
        IDtoClass = ID[0:8]
        self.user_control.userId.setText(ID)
        self.user_control.classNum.setText(IDtoClass)
        self.user_control.showUserImage()
        self.user_control.userclassNum.setText(IDtoClass)
        self.user_control.show()